package com.itqf.controller;

import cn.dsna.util.images.ValidateCode;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/10
 * @Time: 下午3:42
 */
@Controller
public class MyController {


    @RequestMapping(value = "/findOne",produces = "application/json;charset=utf-8")
    @ResponseBody
    public User find(@RequestBody User user){
       // = new User();
//        user.setId(1);
//        user.setName("张三");
//        user.setBirthdate(new Date());
//        user
        System.out.println(user);
        //System.out.println(10/0);

        return user;
    }

    @RequestMapping("/doLogin")
    public  String  doLogin(String username, String password,HttpSession session){

        //查库比较
        User user = new User();
        user.setId(1);
        user.setName("admin");
        if (username.equals("admin")){

            session.setAttribute("user",user);
        }

        return "welcome.jsp";
    }


    @RequestMapping("/code")
    public  void  code(HttpServletResponse response){
        ValidateCode code = new ValidateCode(100,30,5,5);
        String text = code.getCode();
        try {
            ImageIO.write(code.getBuffImg(),"jpg",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @RequestMapping("/showCart")
    public  String showCart(HttpServletRequest request){
        request.setAttribute("cart","购物车信息");

        return "/WEB-INF/jsp/cart.jsp";
    }

    @RequestMapping("/showOrder")
    public  String showOrder(HttpServletRequest request){
        request.setAttribute("order","订单信息");

        return "/WEB-INF/jsp/order.jsp";
    }

}
